package com.example.hotelapp.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.hotelapp.Base.BaseActivity;
import com.example.hotelapp.Bean.ModifyPwdBean;
import com.example.hotelapp.Bean.UserLoginBean;
import com.example.hotelapp.R;
import com.example.hotelapp.Utils.AppNetConfig;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static com.example.hotelapp.Utils.MyCommonUtils.ToastShow;
import static com.example.utils.CommonUtils.Delay3s;
import static com.example.utils.CommonUtils.OKPost;
import static com.example.utils.CommonUtils.OKPostValue;
import static com.example.utils.CommonUtils.SharedInfo;
import static com.example.utils.CommonUtils.setSystemBarColor;

/**
 * 修改个人信息页
 */
public class ModifyInfoActivity extends BaseActivity {

    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.password2)
    EditText password2;
    @BindView(R.id.email)
    EditText email;
    private Activity activity;

    /**
     * 加载布局
     */
    @Override
    protected int setLayoutId() {
        return R.layout.activity_modify;
    }

    /**
     * 初始化
     */
    @Override
    protected void init() {
        activity = this;
        setSystemBarColor(this, R.color.white);
        InitView();
    }

    /**
     * 加载个人信息
     */
    private void InitView() {
        String id = SharedInfo(activity, "GetUid", "id");
        OKPostValue("id", id, AppNetConfig.Login, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //获取返回的json数据并解析
                String json = response.body().string();
                System.out.println(json);
                UserLoginBean databean = new Gson().fromJson(json, UserLoginBean.class);
                int code = databean.getCode();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (code == 1) {
                            if (databean.getData().get(0).getMemberName() != null) {
                                name.setText(databean.getData().get(0).getMemberName());
                            }
                            if (databean.getData().get(0).getPhone() != null) {
                                phone.setText(databean.getData().get(0).getPhone());
                            }
                            if (databean.getData().get(0).getSex() != null) {
                                password2.setText(databean.getData().get(0).getSex());
                            }
                            if (databean.getData().get(0).getCertificateNumber() != null) {
                                email.setText(databean.getData().get(0).getCertificateNumber());
                            }
                        }
                    }
                });
            }
        });
    }

    /**
     * 修改个人信息
     */
    private void ModifyActivity() {
        String names = name.getText().toString().trim();
        String sex = password2.getText().toString().trim();
        String idcard = email.getText().toString().trim();
        String id = SharedInfo(activity, "GetUid", "id");
        if (names.isEmpty() || sex.isEmpty() || idcard.isEmpty()) {
            ToastShow(activity, "请填写完整信息");
        } else if (idcard.length() != 18) {
            ToastShow(activity, "请输入有效的身份证号");
        } else {
            HashMap<String, String> map = new HashMap<>();
            map.put("memberName", names);
            map.put("id", id);
            map.put("certificateNumber", idcard);
            map.put("sex", sex);
            OKPost(map, AppNetConfig.ModifyPwd, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastShow(activity, "服务器出错，请稍后再试");
                        }
                    });
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    //获取返回的json数据并解析
                    String json = response.body().string();
                    System.out.println(json);
                    ModifyPwdBean databean = new Gson().fromJson(json, ModifyPwdBean.class);
                    int code = databean.getCode();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (code == 1) {
                                ToastShow(activity, "修改成功");
                                Delay3s(activity);
                            } else {
                                ToastShow(activity, "修改失败");
                            }
                        }
                    });
                }
            });
        }
    }

    /**
     * 点击事件
     */
    @OnClick({R.id.iv_fanhui, R.id.btn_ook})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_fanhui:
                finish();
                break;
            case R.id.btn_ook:
                ModifyActivity();
                break;
        }
    }
}
